package com.mobialia.socialmediademo;

import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.FacebookError;

public class FacebookAuthActivity extends Activity implements Runnable, Facebook.DialogListener {

	public static String TAG = "FacebookProviderActivity";
	final int ACTIVITY_CODE = 777;
	
	Thread thread;
	ProgressDialog loadingDialog;

	final String appId = "219172308103195";
	final String[] PERMISSIONS = new String[] { "user_birthday" };

	Facebook fb = new Facebook(appId);

	boolean onActivityResult = false;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate()");
		onActivityResult = false;
	}

	public void onResume() {
		super.onResume();
		Log.d(TAG, "onResume()");

		loadingDialog = ProgressDialog.show(this, "", getResources().getText(R.string.loading), true, true);
		loadingDialog.show();

		if (!onActivityResult) {
			fb.authorize(this, PERMISSIONS, ACTIVITY_CODE, this);
		}
	}

	public void onPause() {
		super.onPause();
		loadingDialog.dismiss();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		fb.setStatus(ACTIVITY_CODE, this);
		fb.authorizeCallback(requestCode, resultCode, data);
		onActivityResult = true;
	}

	public void onCancel() {
		Log.d(TAG, "login canceled");
		finish();
	}

	public void onComplete(Bundle values) {
		Log.d(TAG, "onComplete()");

		thread = new Thread(this);
		thread.start();
	}

	public void onError(DialogError e) {
		Log.d(TAG, "dialog error: " + e);
		Toast.makeText(FacebookAuthActivity.this, R.string.error, Toast.LENGTH_SHORT).show();
		finish();
	}

	public void onFacebookError(FacebookError e) {
		Log.d(TAG, "facebook error: " + e);
		Toast.makeText(FacebookAuthActivity.this, R.string.error, Toast.LENGTH_SHORT).show();
		finish();
	}

	public void run() {
		try {		
			String res = fb.request("/me");

			Log.d(TAG, res);

			JSONObject object = new JSONObject(res);
			Log.d(TAG, object.getString("id"));
			Log.d(TAG, object.getString("name"));
			Log.d(TAG, object.getString("gender"));
			Log.d(TAG, object.getString("birthday"));
			
	    	SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
			SharedPreferences.Editor editor = sharedPrefs.edit();
			editor.putString("facebook_id", object.getString("id"));
			editor.putString("facebook_name", object.getString("name"));
			editor.putString("facebook_gender", object.getString("gender"));
			editor.putString("facebook_birthday", object.getString("birthday"));
			editor.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finish();
	}
}